-
Notifications
You must be signed in to change notification settings - Fork 21.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add safe html support to arrays of translations #32361
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
1734cc0
to
98c1aae
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this a little hard to grok, do you have a before/after from your app? Or can you make one?
@@ -85,8 +85,11 @@ def translate(key, options = {}) | |||
end | |||
end | |||
translation = I18n.translate(scope_key_by_partial(key), html_safe_options.merge(raise: i18n_raise)) | |||
|
|||
translation.respond_to?(:html_safe) ? translation.html_safe : translation | |||
if translation.is_a?(Array) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I18n.translate
return enumerables other than just Array? E.g. would respond_to?(:map)
be clearer/better?
@@ -164,7 +164,8 @@ def test_translate_with_html_count | |||
assert_equal "<a>Other <One></a>", translate(:'translations.count_html', count: "<One>") | |||
end | |||
|
|||
def test_translation_returning_an_array_ignores_html_suffix | |||
def test_translate_marks_array_of_translations_with_a_html_safe_suffix_as_safe_html | |||
assert_equal true, translate(:'translations.array_html').all? { |element| element.html_safe? } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally prefer Ruby's definition of true and wouldn't explicitly assert it as the return value.
Think this would be clearer as:
translate(:'translations.array_html').tap do |translated|
assert_equal %w( foo bar ), translated
assert translated.all?(&:html_safe?)
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the other tests I've seen on this file use assert_equal true
so I supposed it was preferred. I will update it.
Hello @kaspth, could you take another look at this? |
Nice! Needs a squash down to 1 commit. Then a changelog entry. We probably don't need the changes in the .sqlite file. |
2c3c30e
to
42c3537
Compare
Great, thanks! |
Summary
The
translate
method ignores the_html
suffix, which leads developers to overusehtml_safe
in places where it shouldn't be used. This PR adds safe html support to arrays of translations whose key includes the_html
suffix.